Micron Document
<!DOCTYPE html>
<html class="client-nojs vector-feature-language-in-header-enabled vector-feature-language-in-main-page-header-disabled vector-feature-page-tools-pinned-disabled vector-feature-toc-pinned-clientpref-0 vector-toc-not-available vector-feature-main-menu-pinned-disabled vector-feature-limited-width-clientpref-1 vector-feature-limited-width-content-enabled vector-feature-custom-font-size-clientpref-1 vector-feature-appearance-pinned-clientpref-0 vector-feature-night-mode-enabled skin-theme-clientpref-os vector-sticky-header-enabled" lang="fr" dir="ltr"><head>
<meta charset="UTF-8">
<title>Cython</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="icon" type="image/png" href="./_res_/favicon.png">
<link rel="canonical" href="https://fr.wikipedia.org/wiki/Cython"> <link href="./_mw_/ext.cite.styles.css" rel="stylesheet" type="text/css">
<link href="./_mw_/ext.pygments.css" rel="stylesheet" type="text/css">
<link href="./_mw_/ext.wikimediamessages.styles.css" rel="stylesheet" type="text/css">
<link href="./_mw_/skins.vector.icons.css" rel="stylesheet" type="text/css">
<link href="./_mw_/skins.vector.search.codex.styles.css" rel="stylesheet" type="text/css">
<link href="./_mw_/skins.vector.styles.css" rel="stylesheet" type="text/css">
<meta name="ResourceLoaderDynamicStyles" content="">
<link rel="stylesheet" type="text/css" href="./_mw_/site.styles.css">
<link rel="stylesheet" type="text/css" href="./_mw_/noscript.css">
<link rel="stylesheet" type="text/css" href="./_res_/footer.css">
<link rel="stylesheet" type="text/css" href="./_res_/vector-2022.css">
</head>
<body class="skin--responsive skin-vector skin-vector-search-vue mediawiki ltr sitedir-ltr mw-hide-empty-elt ns-0 ns-subject page-Cython rootpage-Cython skin-vector-2022 action-view">
<div class="mw-page-container">
<div class="mw-page-container-inner">
<div class="mw-content-container">
<main id="content" class="mw-body">
<header class="mw-body-header vector-page-titlebar">
<h1 id="firstHeading" class="firstHeading mw-first-heading"><span class="mw-page-title-main">Cython</span></h1>
</header>
<a id="top"></a>
<div id="bodyContent" class="vector-body ve-init-mw-desktopArticleTarget-targetContainer" aria-labelledby="firstHeading" data-mw-ve-target-container="">
<div id="contentSub">
<div id="mw-content-subtitle"></div>
</div>
<div id="mw-content-text" class="mw-body-content mw-content-ltr" lang="fr" dir="ltr"><div class="mw-content-ltr mw-parser-output" lang="fr" dir="ltr">
<p class="mw-empty-elt">
</p>
<table class="infobox_v2 infobox infobox--frwiki noarchive">
<tbody><tr>
<td colspan="2" class="entete informatique" style="background-color:#EEEEEE;color:black;">Cython<style data-mw-deduplicate="TemplateStyles:r228712691">
/* start https://fr.wikipedia.org/ */


.mw-parser-output .entete.informatique{background-image:url("./_mw_/Picto.nfo.png")}


/* end https://fr.wikipedia.org/ */
</style>
</td></tr>
<tr><td colspan="3" style="text-align:center; line-height: 1.5em;"> </td></tr>
<tr>
<td colspan="3" style="text-align:center; line-height: 1.5em;"> </td></tr>
<tr>
<td colspan="2"><hr style="height:2px; color:inherit; background-color:#EEEEEE;"></td></tr>
<tr>
<th scope="row">Date de première version
</th>
<td>2007
</td>
</tr>
<tr>
<th scope="row"><a href="Paradigme_(programmation)" title="Paradigme (programmation)">Paradigme</a>
</th>
<td><a href="Programmation_imp%C3%A9rative" title="Programmation impérative">Programmation impérative</a>, <a href="Programmation_proc%C3%A9durale" title="Programmation procédurale">procédurale</a>, structurée
</td>
</tr>
<tr>

<th scope="row">Auteur
</th>
<td>Greg Ewing
</td>
</tr>
<tr>

<th scope="row">Développeur
</th>
<td>Stefan Behnel, Robert Bradshaw, Lisandro Dalcín, Mark Florisson, Vitja Makarov, Dag Sverre Seljebotn
</td>
</tr>
<tr>

<th scope="row"><a href="Version_d'un_logiciel" title="Version d'un logiciel">Dernière version</a>
</th>
<td><span class="wd_p348">3.1.4 (<time class="nowrap date-lien" datetime="2025-09-16" data-sort-value="2025-09-16"><a href="16_septembre" title="16 septembre">16</a> <a href="Septembre_2025" title="Septembre 2025">septembre</a> <a href="2025" title="2025">2025</a></time>)<sup id="cite_ref-wikidata-13b29af4596a434a3726c3e83a1ec549f811e4dd_1-1" class="reference"><a href="#cite_note-wikidata-13b29af4596a434a3726c3e83a1ec549f811e4dd-1"><span class="cite-bracket">[</span>1<span class="cite-bracket">]</span></a></sup></span>
</td>
</tr>
<tr>

<th scope="row">Typage
</th>
<td><a href="Typage_statique" title="Typage statique">statique</a>, <a href="Typage_faible" class="mw-redirect" title="Typage faible">faible</a>
</td>
</tr>
<tr>


<th scope="row">Influencé par
</th>
<td><a href="Python_(langage)" title="Python (langage)">Python</a>
</td>
</tr>
<tr>
<th scope="row">A influencé
</th>
<td>...
</td>
</tr>
<tr>
<th scope="row">Implémentations
</th>
<td><a rel="nofollow" class="external free" href="http://cython.org">http://cython.org</a>
</td>
</tr>
<tr>
<th scope="row"><a href="Langage_de_programmation" title="Langage de programmation">Écrit en</a>
</th>
<td><span class="wd_p277"><a href="Python_(langage)" title="Python (langage)">Python</a></span>
</td>
</tr>
<tr>


<th scope="row"><a href="Licence_de_logiciel" title="Licence de logiciel">Licence</a>
</th>
<td><span class="wd_p275"><a href="Licence_Apache" title="Licence Apache">Licence Apache 2.0</a><sup id="cite_ref-wikidata-710766228e13f03fb0b9a203a7288ce6762e0252_2-0" class="reference"><a href="#cite_note-wikidata-710766228e13f03fb0b9a203a7288ce6762e0252-2"><span class="cite-bracket">[</span>2<span class="cite-bracket">]</span></a></sup> et <a href="Licence_Apache" title="Licence Apache">licence Apache</a><sup id="cite_ref-wikidata-5fefc14c3ac24a1369513e7a8b98f5ef507e3fde_3-0" class="reference"><a href="#cite_note-wikidata-5fefc14c3ac24a1369513e7a8b98f5ef507e3fde-3"><span class="cite-bracket">[</span>3<span class="cite-bracket">]</span></a></sup></span>
</td>
</tr>
<tr>
<th scope="row"><a href="Site_web" title="Site web">Site web</a>
</th>
<td><span class="wd_p856"><a rel="nofollow" class="external text" href="https://cython.org">cython.org</a></span>
</td>
</tr>
<tr>


</tr>
</tbody></table>
<p><b>Cython</b> est un <a href="Langage_de_programmation" title="Langage de programmation">langage de programmation</a> et un <a href="Compilateur" title="Compilateur">compilateur</a> qui simplifient l'écriture d'extensions compilées pour <a href="Python_(langage)" title="Python (langage)">Python</a>. La syntaxe du langage est très similaire à Python mais il supporte en plus un sous-ensemble du langage <a href="C_(langage)" title="C (langage)">C</a>/<a href="C%2B%2B" title="C++">C++</a> (déclarations de <a href="Variable_(informatique)" title="Variable (informatique)">variables</a>, appel de <a href="Routine_(informatique)" title="Routine (informatique)">fonctions</a>, ...).
</p><p>Le premier intérêt de Cython est qu'il produit du code nettement plus performant. Dans des programmes qui nécessitent par exemple la manipulation de grands tableaux, le gain peut aller jusqu'à un facteur 100<sup id="cite_ref-4" class="reference"><a href="#cite_note-4"><span class="cite-bracket">[</span>4<span class="cite-bracket">]</span></a></sup>. Par ailleurs, Cython permet d'écrire des interfaces Python à des bibliothèques externes écrites en C ou C++.
</p><p>Cython est disponible pour la plupart des <a href="Syst%C3%A8me_d'exploitation" title="Système d'exploitation">systèmes d'exploitation</a>.
</p>

<div class="mw-heading mw-heading2"><h2 id="Fonctionnement">Fonctionnement</h2></div>
<p>Le langage Python dès son origine supporte l'écriture de modules écrits en langage C. Cependant, cette procédure est laborieuse et délicate. Le compilateur Cython convertit ses fichiers sources en langage Cython (<a href="Extension_de_nom_de_fichier" title="Extension de nom de fichier">extension</a> .pyx) vers le langage C ou C++. Dans un deuxième temps, ces fichiers C/C++ sont compilés sous forme de bibliothèques prêtes à être utilisées avec Python.
</p><p>Cython automatise entre autres&nbsp;:
</p>
<ul><li>la conversion automatique entre types Python et types C&nbsp;;</li>
<li>la compatibilité Python 2/Python 3&nbsp;;</li>
<li>la gestion des erreurs.</li></ul>
<p>Le compilateur Cython est lui-même écrit en Python.
</p>
<div class="mw-heading mw-heading2"><h2 id="Histoire">Histoire</h2></div>
<p>Cython est né comme sous-projet du <a href="Logiciel_libre" title="Logiciel libre">logiciel libre</a> de calcul formel <a href="SageMath" title="SageMath">SageMath</a> en tant que <a href="Fork_(d%C3%A9veloppement_logiciel)" title="Fork (développement logiciel)">fork</a> du langage <a href="Pyrex_(langage)" title="Pyrex (langage)">Pyrex</a>. SageMath est un des logiciels qui utilise le plus de fonctionnalités de Cython.
</p><p>Les programmes écrits en Cython possèdent l'extension .pyx. Dans sa forme la plus simple, le code source de Cython ressemble exactement au code source de Python. Cependant, alors que les variables, dans Python standard, sont typées dynamiquement&nbsp;; le typage est facultatif. Ce qui permet d'améliorer les performances et de convertir les boucles Cython en boucles C lorsque cela est possible.
</p><p>
Par exemple&nbsp;: </p><div class="mw-highlight mw-highlight-lang-cython mw-content-ltr" dir="ltr"><pre><span></span><span class="k">def</span><span class="w"> </span><span class="nf">primes</span><span class="p">(</span><span class="nb">int</span> <span class="n">kmax</span><span class="p">):</span> <span class="c"># L'argument sera converti en nombre ou provoquera un TypeError.</span>
<span class="k">cdef</span><span class="w"> </span><span class="kt">int</span> <span class="nf">n</span><span class="p">,</span> <span class="nf">k</span><span class="p">,</span> <span class="nf">i</span><span class="w"> </span><span class="c"># Ces variables sont déclarées avec des types en C.</span>
<span class="k">cdef</span><span class="w"> </span><span class="kt">int</span> <span class="kt">p</span>[1000] # <span class="kt">un</span> <span class="kt">autre</span> <span class="kt">typage</span> <span class="nf">C</span>
<span class="n">result</span> <span class="o">=</span> <span class="p">[]</span> <span class="c"># un typage Python</span>
<span class="k">if</span> <span class="n">kmax</span> <span class="o">&gt;</span> <span class="mf">1000</span><span class="p">:</span>
<span class="n">kmax</span> <span class="o">=</span> <span class="mf">1000</span>
<span class="n">k</span> <span class="o">=</span> <span class="mf">0</span>
<span class="n">n</span> <span class="o">=</span> <span class="mf">2</span>
<span class="k">while</span> <span class="n">k</span> <span class="o">&lt;</span> <span class="n">kmax</span><span class="p">:</span>
<span class="n">i</span> <span class="o">=</span> <span class="mf">0</span>
<span class="k">while</span> <span class="n">i</span> <span class="o">&lt;</span> <span class="n">k</span> <span class="ow">and</span> <span class="n">n</span> <span class="o">%</span> <span class="n">p</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">!=</span> <span class="mf">0</span><span class="p">:</span>
<span class="n">i</span> <span class="o">=</span> <span class="n">i</span> <span class="o">+</span> <span class="mf">1</span>
<span class="k">if</span> <span class="n">i</span> <span class="o">==</span> <span class="n">k</span><span class="p">:</span>
<span class="n">p</span><span class="p">[</span><span class="n">k</span><span class="p">]</span> <span class="o">=</span> <span class="n">n</span>
<span class="n">k</span> <span class="o">=</span> <span class="n">k</span> <span class="o">+</span> <span class="mf">1</span>
<span class="n">result</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">n</span><span class="p">)</span>
<span class="n">n</span> <span class="o">=</span> <span class="n">n</span> <span class="o">+</span> <span class="mf">1</span>
<span class="k">return</span> <span class="n">result</span>
</pre></div>
<div class="mw-heading mw-heading3"><h3 id="Utilisation_dans_le_bloc-notes_IPython/Jupyter"><span id="Utilisation_dans_le_bloc-notes_IPython.2FJupyter"></span>Utilisation dans le bloc-notes IPython/Jupyter</h3></div>
<p>Un moyen plus simple de commencer avec Cython consiste à utiliser la ligne de commande <a href="IPython" title="IPython">IPython</a> (ou via la console python dans le navigateur appelée Jupyter notebook)<sup id="cite_ref-5" class="reference"><a href="#cite_note-5"><span class="cite-bracket">[</span>5<span class="cite-bracket">]</span></a></sup>:
</p>
<div class="mw-highlight mw-highlight-lang-cython mw-content-ltr" dir="ltr"><pre><span></span><span class="n">In</span> <span class="p">[</span><span class="mf">1</span><span class="p">]:</span> <span class="o">%</span><span class="n">load_ext</span> <span class="n">Cython</span>

<span class="n">In</span> <span class="p">[</span><span class="mf">2</span><span class="p">]:</span> <span class="o">%%</span><span class="n">cython</span>
<span class="o">...</span><span class="p">:</span> <span class="k">def</span><span class="w"> </span><span class="nf">f</span><span class="p">(</span><span class="n">n</span><span class="p">):</span>
<span class="o">...</span><span class="p">:</span> <span class="n">a</span> <span class="o">=</span> <span class="mf">0</span>
<span class="o">...</span><span class="p">:</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">n</span><span class="p">):</span>
<span class="o">...</span><span class="p">:</span> <span class="n">a</span> <span class="o">+=</span> <span class="n">i</span>
<span class="o">...</span><span class="p">:</span> <span class="k">return</span> <span class="n">a</span>
<span class="o">...</span><span class="p">:</span>
<span class="o">...</span><span class="p">:</span> <span class="k">cpdef</span><span class="w"> </span><span class="nf">g</span><span class="p">(</span><span class="nb">int</span> <span class="n">n</span><span class="p">):</span>
<span class="o">...</span><span class="p">:</span> <span class="k">cdef</span><span class="w"> </span><span class="kt">long</span> <span class="nf">a</span><span class="w"> </span><span class="o">=</span> <span class="mf">0</span>
<span class="o">...</span><span class="p">:</span> <span class="k">cdef</span><span class="w"> </span><span class="kt">int</span> <span class="nf">i</span>
<span class="o">...</span><span class="p">:</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">n</span><span class="p">):</span>
<span class="o">...</span><span class="p">:</span> <span class="n">a</span> <span class="o">+=</span> <span class="n">i</span>
<span class="o">...</span><span class="p">:</span> <span class="k">return</span> <span class="n">a</span>
<span class="o">...</span><span class="p">:</span>

<span class="n">In</span> <span class="p">[</span><span class="mf">3</span><span class="p">]:</span> <span class="o">%</span><span class="n">timeit</span> <span class="n">f</span><span class="p">(</span><span class="mf">1000000</span><span class="p">)</span>
<span class="mf">10</span> <span class="n">loops</span><span class="p">,</span> <span class="n">best</span> <span class="n">of</span> <span class="mf">3</span><span class="p">:</span> <span class="mf">26.5</span> <span class="n">ms</span> <span class="n">per</span> <span class="n">loop</span>

<span class="n">In</span> <span class="p">[</span><span class="mf">4</span><span class="p">]:</span> <span class="o">%</span><span class="n">timeit</span> <span class="n">g</span><span class="p">(</span><span class="mf">1000000</span><span class="p">)</span>
<span class="mf">1000</span> <span class="n">loops</span><span class="p">,</span> <span class="n">best</span> <span class="n">of</span> <span class="mf">3</span><span class="p">:</span> <span class="mf">279</span> <span class="err">µ</span><span class="n">s</span> <span class="n">per</span> <span class="n">loop</span>
</pre></div>
<p>ce qui donne une amélioration de 95 fois par rapport à la version en Python pur. Plus de détails sur le sujet dans la page officielle de démarrage rapide<sup id="cite_ref-6" class="reference"><a href="#cite_note-6"><span class="cite-bracket">[</span>6<span class="cite-bracket">]</span></a></sup>.
</p>
<div class="mw-heading mw-heading2"><h2 id="Notes_et_références"><span id="Notes_et_r.C3.A9f.C3.A9rences"></span>Notes et références</h2></div>
<div class="references-small decimal" style=""><div class="mw-references-wrap"><ol class="references">
<li id="cite_note-wikidata-13b29af4596a434a3726c3e83a1ec549f811e4dd-1"><span class="reference-text"><span class="ouvrage">«&nbsp;<a rel="nofollow" class="external text" href="https://github.com/cython/cython/releases/tag/3.1.4"><cite style="font-style:normal;"><span class="lang-en" lang="en">Release 3.1.4</span></cite></a>&nbsp;», <time class="nowrap" datetime="2025-09-16" data-sort-value="2025-09-16">16 septembre 2025</time> <small style="line-height:1em;">(consulté le <time class="nowrap" datetime="2025-10-13" data-sort-value="2025-10-13">13 octobre 2025</time>)</small></span></span>
</li>
<li id="cite_note-wikidata-710766228e13f03fb0b9a203a7288ce6762e0252-2"><span class="mw-cite-backlink"><a href="#cite_ref-wikidata-710766228e13f03fb0b9a203a7288ce6762e0252_2-0">↑</a> </span><span class="reference-text"><span class="ouvrage">«&nbsp;<a rel="nofollow" class="external text" href="https://github.com/cython/cython/blob/master/LICENSE.txt"><cite style="font-style:normal;">https://github.com/cython/cython/blob/master/LICENSE.txt</cite></a>&nbsp;» <small style="line-height:1em;">(consulté le <time class="nowrap" datetime="2020-02-02" data-sort-value="2020-02-02">2 février 2020</time>)</small></span></span>
</li>
<li id="cite_note-wikidata-5fefc14c3ac24a1369513e7a8b98f5ef507e3fde-3"><span class="mw-cite-backlink"><a href="#cite_ref-wikidata-5fefc14c3ac24a1369513e7a8b98f5ef507e3fde_3-0">↑</a> </span><span class="reference-text"><span class="ouvrage">«&nbsp;<a rel="nofollow" class="external text" href="http://cython.org/"><cite style="font-style:normal;"><span class="lang-en" lang="en">Cython: C-Extensions for Python</span></cite></a>&nbsp;» <small style="line-height:1em;">(consulté le <time class="nowrap" datetime="2018-07-08" data-sort-value="2018-07-08">8 juillet 2018</time>)</small></span></span>
</li>
<li id="cite_note-4"><span class="mw-cite-backlink"><a href="#cite_ref-4">↑</a> </span><span class="reference-text"><span class="ouvrage"><abbr class="abbr indicateur-langue" title="Langue : anglais">(en)</abbr> «&nbsp;<a rel="nofollow" class="external text" href="https://cython.readthedocs.io/en/latest/src/tutorial/numpy.html"><cite style="font-style:normal;" lang="en">Working with NumPy — Cython 0.28a0 documentation</cite></a>&nbsp;», sur <span class="italique">cython.readthedocs.io</span> <small style="line-height:1em;">(consulté le <time class="nowrap" datetime="2017-11-27" data-sort-value="2017-11-27">27 novembre 2017</time>)</small></span></span>
</li>
<li id="cite_note-5"><span class="mw-cite-backlink"><a href="#cite_ref-5">↑</a> </span><span class="reference-text"><span class="ouvrage" id="Lutz"><span class="ouvrage" id="Mark_Lutz">Mark Lutz, <cite class="italique">Learning Python, 5th Edition</cite> <small style="line-height:1em;">(<a rel="nofollow" class="external text" href="https://python.engineering/learning-python-5th-edition/">lire en ligne</a>)</small><span class="Z3988" title="ctx_ver=Z39.88-2004&amp;rft_val_fmt=info%3Aofi%2Ffmt%3Akev%3Amtx%3Abook&amp;rft.genre=book&amp;rft.btitle=Learning+Python%2C+5th+Edition&amp;rft.aulast=Lutz&amp;rft.aufirst=Mark&amp;rft_id=https%3A%2F%2Fpython.engineering%2Flearning-python-5th-edition%2F&amp;rfr_id=info%3Asid%2Ffr.wikipedia.org%3ACython"></span></span></span></span>
</li>
<li id="cite_note-6"><span class="mw-cite-backlink"><a href="#cite_ref-6">↑</a> </span><span class="reference-text"><span class="ouvrage">«&nbsp;<a rel="nofollow" class="external text" href="https://cython.readthedocs.io/en/latest/src/quickstart/build.html"><cite style="font-style:normal;">Building Cython code</cite></a>&nbsp;», sur <span class="italique">cython.readthedocs.io</span> <small style="line-height:1em;">(consulté le <time class="nowrap" datetime="2017-04-24" data-sort-value="2017-04-24">24 avril 2017</time>)</small></span></span>
</li>
</ol></div>
</div>
<div class="mw-heading mw-heading2"><h2 id="Liens_externes">Liens externes</h2></div>
<ul><li><a rel="nofollow" class="external text" href="http://cython.org">Site Cython officiel (en anglais)</a></li>
<li><a rel="nofollow" class="external text" href="https://github.com/cython/cython">Site de développement (github)</a></li></ul>
<div class="navbox-container" style="clear:both;">

</div>
<ul id="bandeau-portail" class="bandeau-portail"><li><span class="bandeau-portail-element"><span class="bandeau-portail-icone"><span class="noviewer" typeof="mw:File"></span></span> <span class="bandeau-portail-texte">Portail de l’informatique</span> </span></li> <li><span class="bandeau-portail-element"><span class="bandeau-portail-icone"><span class="noviewer" typeof="mw:File"></span></span> <span class="bandeau-portail-texte">Portail de la programmation informatique</span> </span></li> </ul></div><!--htdig_noindex--><div><div class="zim-footer">
Cet article est issu de <a class="external text" title="Dernière modification le 2024-05-25" href="https://fr.wikipedia.org/wiki/?title=Cython&amp;oldid=215379198">Wikipédia</a>. Sauf mention contraire, le texte est disponible sous <a class="external text" href="https://creativecommons.org/licenses/by-sa/4.0/deed.fr">Creative Commons Attribution-Share Alike 4.0</a>. Des conditions supplémentaires peuvent s’appliquer aux fichiers multimédias.
</div>
</div><!--/htdig_noindex--></div>
</div>
</main>
</div>
</div>
</div>
<script src="./_webp_/webpHandler.js"></script>

</body></html>